<?php
class NewsHomeHotForm extends Form
{
	function __construct()
	{				
		Form::Form('NewsHomeHotForm');
	}
	
	function on_submit()
	{
		if(User::is_admin())
		{
			$submit 		= Url::get('submit');
			
			if($submit == "Cập nhật")
			{
				$title_news 	= Url::get('title_news');
				$news_ids 		= Url::get('news_ids');
					
				$config_update = array(
											'title' 	=> $title_news,
											'news_ids'	=> implode(',',explode(',',$news_ids)),
											);
											
				###########################
				if(isset(CGlobal::$configs['NewsHomeHot']))
					DB::update('configs',array("conf_val"=>addslashes(serialize($config_update))),"conf_key='NewsHomeHot'", __FILE__." Dòng:".__LINE__);
				else
					DB::insert('configs',array("conf_key"=>'NewsHomeHot',"conf_val"=>addslashes(serialize($config_update))), __FILE__." Dòng:".__LINE__);
				
				AZLib::get_config(0,1);
			}
			
			if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']){
				$referer = $_SERVER['HTTP_REFERER'];
			}
			else{
				$referer = $_SERVER['REQUEST_URI'];
			}
			
			Url::redirect_url($referer);
		}
		
		Url::redirect_url($_SERVER['REQUEST_URI']);
	}
	
	function draw()
	{
		global $display;
		
		$news_ids 	= '';
		$hot_newss 	= array();
		
		$configs = @CGlobal::$configs['NewsHomeHot']['conf_val'];
		
		if($configs)
		{
			$configs = unserialize(stripslashes($configs));
			
			$title_news 	= $configs['title'];
			$news_ids 		= $configs['news_ids'];
		}
		else
		{
			$title_news 	= "TIN HOT";
			$news_ids 	= '';
		}
		
		if($news_ids != '')
		{		
			$re	= DB::query("SELECT id, title, news_catid FROM news WHERE id IN($news_ids) AND status = 1 ORDER BY find_in_set(id,'$news_ids')");
			
			if($re)
			{	
				while ($news = mysql_fetch_assoc($re))
				{	
					$news['title'] 	= stripslashes($news['title']);
					
					$news['url'] 	= Url::build('news_detail',array('news_catid'=>$news['news_catid'],'news_id'=>$news['id'],'azname'=>AZLib::safe_title($news['title'])));
					
					$hot_newss[$news['id']] = $news;
				}
			}
		}
		
		if(User::is_admin())
		{
			$display->add("is_admin",1);
			$display->add("msg",$this->showFormErrorMessages(1));
			$display->add('begin_form',$this->beginForm(true,'post',false,false,1));
			$display->add('end_form',$this->endForm(true));
		}
		
		$display->add('title_news',$title_news);
		$display->add('news_ids',$news_ids);
		$display->add('hot_newss',$hot_newss);
		
		$display->output("NewsHomeHot");
	}
}
?>